package com.black.common.upload.serializer;


import com.black.common.upload.config.MyOosConfig;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @author linwenfeng
 */
public class ImgJsonSerializer extends JsonSerializer<String> {


    private static final String EMPTY = "";

    private static final String COMMA = ",";

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (StringUtils.isEmpty(value)) {
            gen.writeString(EMPTY);
            return;
        }
        String[] imgList = value.split(COMMA);
        StringBuilder sb = new StringBuilder();
        for (String img : imgList) {
            sb.append(MyOosConfig.domain).append(img).append(COMMA);
        }
        sb.deleteCharAt(sb.length()-1);
        gen.writeString(sb.toString());
    }
}
